import { ChannelList } from '@/types/data'
import { ChannelAction } from '@/types/store'

type State = {
  allChannel: ChannelList
  userChannel: ChannelList
  active: string // 当前选中的频道的id
}

const initState: State = {
  allChannel: [],
  userChannel: [],
  active: '0' // 当前选中的频道的id
}
const channel = (state = initState, action: ChannelAction) => {
  console.log('channel---reducer', action)
  if (action.type === 'channel/getChannel') {
    return { ...state, userChannel: action.payload }
  } else if (action.type === 'channel/setActive') {
    return { ...state, active: action.id }
  } else if (action.type === 'channel/saveAllChannels') {
    return { ...state, allChannel: action.payload }
  }
  return state
}

export default channel
